from collections import defaultdict


class Solution:
    def topKFrequent(self, nums: list, k: int) -> list:
        mid_dic = defaultdict(int)
        for i in nums:
            # if i not in mid_dic:
            #     mid_dic[i] = 1
            # else:
            #     mid_dic[i] += 1
            mid_dic[i] += 1
        res_list = [i for i in mid_dic]
        res_list.sort(reverse=True, key=lambda x: mid_dic[x])
        return res_list[:k]
